package com.yf.service.rongcloud.impl;

import com.yf.dao.rongcloud.UsersDao;
import com.yf.entity.WxUser;
import com.yf.entity.rongcloud.UsersEntity;
import com.yf.response.RRException;
import com.yf.service.impl.BaseServiceImpl;
import com.yf.service.rongcloud.UsersService;
import com.yf.utils.RongCloudUtils;
import com.yf.vo.rongcloud.TokenVO;
import io.rong.models.Result;
import io.rong.models.group.GroupMember;
import io.rong.models.response.GroupUser;
import io.rong.models.response.GroupUserQueryResult;
import io.rong.models.response.TokenResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Mon Oct 22 10:50:05 CST 2018
 */
@Service
@Slf4j
public class UsersServiceImpl extends BaseServiceImpl<UsersEntity> implements UsersService {
    @Autowired
    private UsersDao usersDao;



    /**内部方法**/


    /**外部方法实现**/
    @Override
    public UsersEntity findByUnionId(String unionId) {
        List<UsersEntity> list = usersDao.findByUnionId(unionId);
        if(list==null||list.isEmpty()){
            return null;
        }
        if(list.size()>1){
            log.error("账号异常unionId不唯一:"+unionId);
            throw new RRException(unionId+"账号不唯一");
        }
        return list.get(0);
    }

    //微信用户登陆，返回融云的token
    @Override
    public TokenVO registerUser(WxUser wxUser) {
        TokenVO tokenVO;
        //判断微信用户有没有登陆过
        UsersEntity usersEntity = findByUnionId(wxUser.getUnionId());
        //首次登陆
        if(usersEntity == null){
            UsersEntity user = new UsersEntity(wxUser.getNickName(),wxUser.getHeadImgUrl(),wxUser.getUnionId());
            usersDao.save(user);
            //获取融云那边的token
            TokenResult tokenResult = RongCloudUtils.getRongCloudToken(String.valueOf(user.getId()),user.getNickname(),user.getPortraituri());
            //保存融云返回的token，永久有效
            user.setRongcloudtoken(tokenResult.getToken());
            usersDao.update(user);
            tokenVO = new TokenVO(user.getId(),user.getRongcloudtoken());
        }else {
            tokenVO = new TokenVO(usersEntity.getId(),usersEntity.getRongcloudtoken());
        }

        return tokenVO;
    }

    @Override
    public void joinGroup(Long groupId,Long userId) {
        //先判断有没有加群
        boolean isJoin = false;
        GroupUserQueryResult groupUserQueryResult = RongCloudUtils.getGroup(String.valueOf(groupId));
        if(!ObjectUtils.isEmpty(groupUserQueryResult)){
            List<GroupUser> list = groupUserQueryResult.getMembers();
            if(!list.isEmpty()){
                for (GroupUser groupUser : list) {
                    if(groupUser.getId().equals(userId)){
                        isJoin = true;
                        break;
                    }
                }
            }
        }
        //若没有加群,加群
        if(!isJoin){
            GroupMember[] groupMembers = new GroupMember[1];
            groupMembers[0].setId(String.valueOf(userId)).setGroupId(String.valueOf(groupId));
            RongCloudUtils.joinGroup(groupMembers,String.valueOf(groupId));
        }
    }
}
